﻿//---------------------------------------------------------------------------------------------
//  Copyright 2010 Jeremy Palenchar
//  http://identitynotes.palenchar.net
//
//  This file is part of the Identity Notes Identity Notes Sample Custom Activity Library.
//
//  The Identity Notes Sample Custom Activity Library is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  The Identity Notes Sample Custom Activity Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with The Identity Notes Sample Custom Activity Library.  If not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Net.Mail;
using System.Net;
using System.Collections.Generic;

namespace IdentityNotes.Examples.CustomActivityLibrary
{
	public partial class SendEmailActivity: Activity
	{
		public SendEmailActivity()
		{
            InitializeComponent();
        }

        public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(string), typeof(SendEmailActivity));

        [FIMActivityUI(ControlType.String,55,1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Properties")]
        public string To
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.SendEmailActivity.ToProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.SendEmailActivity.ToProperty, value);
            }
        }

        public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(string), typeof(SendEmailActivity));

        [FIMActivityUI(ControlType.String,55,1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public string From
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.SendEmailActivity.FromProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.SendEmailActivity.FromProperty, value);
            }
        }

        public static DependencyProperty SubjectProperty = DependencyProperty.Register("Subject", typeof(string), typeof(SendEmailActivity));

        [FIMActivityUI(ControlType.String,255,1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public string Subject
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.SendEmailActivity.SubjectProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.SendEmailActivity.SubjectProperty, value);
            }
        }

        public static DependencyProperty EmailBodyProperty = DependencyProperty.Register("EmailBody", typeof(string), typeof(SendEmailActivity));

        [FIMActivityUI(ControlType.String,600,10)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public string EmailBody
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.SendEmailActivity.EmailBodyProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.SendEmailActivity.EmailBodyProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(
            ActivityExecutionContext executionContext)
        {
            DateTime now = new DateTime();

            now = DateTime.Now;

            SendEmail(this.To, this.From, this.Subject, this.EmailBody);
            
            return base.Execute(executionContext);
        }

        public void SendEmail(string To, string From, string Subject, string Body)
        {
            //Setup mail message
            MailMessage msg = new MailMessage(From, To, Subject, Body);
            msg.IsBodyHtml = false;

            //Send
            SmtpClient client = new SmtpClient("smtp.gmail.com",587);
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("sendilm@palenchar.net", "ILMRocks!");
            client.EnableSsl = true;
            client.Send(msg);
        }
	}
}
